package com.aizuda.boot.modules.business.counterparty.service.impl;

import com.aizuda.boot.modules.business.counterparty.entity.ChangeRecord;
import com.aizuda.boot.modules.business.counterparty.entity.vo.ChangeRecordVO;
import com.aizuda.boot.modules.business.counterparty.entity.vo.OperationUserVO;
import com.aizuda.boot.modules.business.counterparty.mapper.ChangeRecordMapper;
import com.aizuda.boot.modules.business.counterparty.service.ChangeRecordService;
import com.aizuda.boot.modules.business.permission.domain.response.EmployeeShowVO;
import com.aizuda.boot.modules.system.entity.SysUser;
import com.aizuda.boot.modules.system.mapper.SysUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author sangyirong
 * @ClassName ChangeRecordServiceImpl
 * @Version 1.0
 * @Date 2025/7/12 13:30
 */
@Service
public class ChangeRecordServiceImpl extends ServiceImpl<ChangeRecordMapper, ChangeRecord> implements ChangeRecordService {


    @Resource
    private SysUserMapper sysUserMapper;

    @Override
    public List<ChangeRecordVO> queryChangeRecords(Integer relId, String type) {
        ArrayList<ChangeRecordVO> result = new ArrayList<>();
        LambdaQueryWrapper<ChangeRecord> wrapper = new LambdaQueryWrapper<>();
        if (Objects.equals("counterparty", type)) {
            wrapper.eq(ChangeRecord::getCounterpartyId, relId);
        } else if (Objects.equals("legal", type)) {
            wrapper.eq(ChangeRecord::getLegalId, relId);
        }
        List<ChangeRecord> changeRecords = list(wrapper.orderByDesc(ChangeRecord::getVersion));
        if (CollectionUtils.isEmpty(changeRecords)){
            return result;
        }
        Map<Integer, List<ChangeRecord>> versionMap = changeRecords.stream().collect(Collectors.groupingBy(ChangeRecord::getVersion));
        Set<Map.Entry<Integer, List<ChangeRecord>>> entries = versionMap.entrySet();
        entries.forEach(entry -> {
            ChangeRecordVO vo = new ChangeRecordVO();
            List<ChangeRecord> changeRecordList = entry.getValue();
            Long createId = changeRecordList.get(0).getCreateId();
            SysUser user = sysUserMapper.selectById(createId);
            OperationUserVO employeeShowVO = new OperationUserVO();
            employeeShowVO.setId(user.getId());
            employeeShowVO.setName(user.getNickName());
            employeeShowVO.setAvatar(user.getAvatar());
            employeeShowVO.setOperateTime(changeRecordList.get(0).getCreateTime());
            vo.setOperator(employeeShowVO);
            HashMap<String, Object> fieldChanges = new HashMap<>();
            HashMap<String, Object> bank = new HashMap<>();
            HashMap<String, Object> attachment = new HashMap<>();
            ArrayList<String> bankAddList = new ArrayList<>();
            ArrayList<String> bankDeleteList = new ArrayList<>();
            ArrayList<String> attachmentAddList = new ArrayList<>();
            ArrayList<String> attachmentDeleteList = new ArrayList<>();
            changeRecordList.forEach(item ->{
                if ("bank".equals(item.getField())){
                    if (item.getNewValue() == null){
                        bankDeleteList.add(item.getOldValue());
                    }
                    if (item.getOldValue() == null){
                        bankAddList.add(item.getNewValue());
                    }
                }if ("attachment".equals(item.getField())){
                    if (item.getNewValue() == null){
                        attachmentDeleteList.add(item.getOldValue());
                    }
                    if (item.getOldValue() == null){
                        attachmentAddList.add(item.getNewValue());
                    }
                }else {
                    HashMap<Object, Object> map = new HashMap<>();
                    map.put("oldValue", item.getOldValue());
                    map.put("newValue", item.getNewValue());
                    fieldChanges.put(item.getField(), map);
                }
            });
            bank.put("addList", bankAddList);
            bank.put("deleteList", bankDeleteList);
            attachment.put("addList", attachmentAddList);
            attachment.put("deleteList", attachmentDeleteList);
            vo.setValues(fieldChanges);
            vo.setBank(bank);
            vo.setAttachments(attachment);
            result.add(vo);
        });
        return result;
    }
}
